﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTest
{
    class AfflicationEnroll
    {
        public static void Upload()
        {
            List<Model> lists = new List<Model>();
            using (SqlConnection Conn = new SqlConnection("Data Source=.;Initial Catalog=Estimate;User ID=sa;Password=123"))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT TOP 1000 a.id,a.[name],a.[code],p.name as parent,a.[level],a.[lontitude],a.[latitude] ,a.[height] FROM [Estimate].[dbo].[Affiliation] a left join [Affiliation] p on a.parent_id = p.id order by level ", Conn))
                {
                    using (SqlDataAdapter Sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        Sda.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                string name = dt.Rows[i]["name"].ToString();
                                string code = dt.Rows[i]["code"].ToString();
                                string lat = dt.Rows[i]["lontitude"].ToString();
                                string lng = dt.Rows[i]["latitude"].ToString();
                                string parent = dt.Rows[i]["parent"].ToString();
                                string height = dt.Rows[i]["height"].ToString();
                                Model m = new Model() { name= name,code=code,latitude = lat,longitude =lng, height=height,parent = parent };
                                lists.Add(m);
                            }
                        }
                    }
                }
            }
            System.Web.Script.Serialization.JavaScriptSerializer Ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            StringBuilder sb = new StringBuilder();
            Ser.Serialize(lists, sb);
            Console.WriteLine(sb.ToString());
            WeChatDataSrc.HttpPost.SendData("http://newqxtweixin.team4.us/data/afflication_enroll/", "data=" + sb.ToString());
        }


        public class Model
        {
            public Model()
            {
                code = ""; name = "";  latitude = ""; longitude = ""; parent = "";  height = "";
            }
            public string code { get; set; }
            public string name { get; set; }
            public string latitude { get; set; }
            public string longitude { get; set; }
            public string parent { get; set; }
            public string height { get; set; }
        }
    }
}
